<style scoped>
    .item-disabled {
        margin-bottom: 0;
    }

    .type-box {
        display: flex;
        flex-flow: row;
    }

    .item-title {
        text-align: center;
    }

</style>
<template>
    <div class="Business">
        <Form ref="formData" :model="formData" :rules="formRules" :label-width="120"
              :disabled="isApprove || pageParams.readOnly">
            <Row>
                <Col :xs="24" :sm="12" :lg="8">
                    <FormItem label="旧到期日" prop="oldDueDate">
                        <DatePicker type="date" v-model="formData.oldDueDate" disabled/>
                    </FormItem>
                </Col>
                <Col :xs="24" :sm="12" :lg="8">
                    <FormItem label="新到期日" prop="newDueDate">
                        <DatePicker type="date" :start-date="new Date(this.formData.oldDueDate)" :options="dateSetting"
                                    v-model="formData.newDueDate" @on-change="calculate"/>
                    </FormItem>
                </Col>
                <Col v-if="this.Obj.roleType" :xs="24" :sm="12" :lg="8">
                    <FormItem label="短信验证码" prop="verifyCode">
                        <Row>
                            <Col span="16">
                                <Input type="text" v-model="formData.verifyCode"/>
                            </Col>
                            <Col span="8" style="text-align: right">
                                <Button type="primary" @click="sendSmgToContract">发送验证码</Button>
                            </Col>
                        </Row>
                    </FormItem>
                </Col>

                <Col :xs="24" :sm="12" :lg="8">
                    <FormItem label="额外利息" prop="extraInterest">
                        <RuiNumber :precision="2" type="currency" v-model="formData.extraInterest" disabled/>
                    </FormItem>
                </Col>
                <Col :xs="24" :sm="12" :lg="8">
                    <FormItem label="手续费" prop="poundage">
                        <RuiNumber :precision="2" type="currency" v-model="formData.poundage" disabled/>
                    </FormItem>
                </Col>
                <!-- <Col :xs="24" :sm="12" :lg="8">
                     <RuiNumber label="合计金额" prop="sumCost">
                         <RuiNumber :precision=2 v-model="formData.sumCost" disabled />
                     </FormItem>
                 </Col>-->
            </Row>
        </Form>
    </div>
</template>
<script>
    import {
        queryBusinessHandle,
        sendMessageInfo,
        viewOrEdit
    } from "_p/basic/api/contract/post-loan-info-change/change-repayment";
    import {isPhoneValiate, verifyErrorToView} from "_p/basic/assets/contract/js/utils";
    import moment from "moment";

    export default {
        props: {
            dataDict: {
                type: Object,
                required: true
            },
            isApprove: {
                type: Boolean,
                require: true
            },
            //对象参数包含：合同号码、角色类型
            Obj: {
                type: Object,
                required: true
            },
            pageParams: {
                type: Object
            }
        },
        data() {
            return {
                indeterminate: false,
                checkAllType: false,
                isSave: false,
                type: "",
                changeSubject: "",
                business: "",
                dateSetting: {
                    disabledDate(date) {
                    }
                },
                formData: {
                    newDueDate: "",
                    realInterestRate: "",
                    remainPrinciple: "",
                    oldDueDate: "",
                    extraInterest: null,
                    poundage: null,
                    sumCost: "",
                    repayChangeLimitFrequency: "",
                    verifyCode: "",
                },
                contactNo: "",
                formRules: {
                    type: [
                        {required: true, message: "请选择一项", trigger: "change"}
                    ],
                    changeSubject: [
                        {required: true, message: "请选择一项", trigger: "change"}
                    ],
                    newPhone: [
                        {
                            required: false,
                            validator: isPhoneValiate,
                            trigger: "blur"
                        }
                    ],
                    verifyCode: [
                        {required: true, message: "请输入", trigger: "change"}
                    ],
                    newWorkPhone: [
                        {
                            required: false,
                            validator: isPhoneValiate,
                            trigger: "blur"
                        }
                    ]
                }
            };
        },
        mounted() {
            this.init();
        },
        methods: {
            init() {
                this.formData = Object.assign({}, this.formData, this.pageParams);
                this.queryBusinessHandle();
            },
            queryBusinessHandle() {
                if (this.pageParams.caseNo) {
                    viewOrEdit(this.pageParams.contractNo, this.pageParams.caseNo).then(res => {
                        if (res.code === "0000") {
                            this.formData = res.data[0];
                        }
                    });

                } else {
                    queryBusinessHandle(this.Obj.contractNo).then(res => {
                        if (res.code === "0000") {
                            this.formData = res.data;
                            viewOrEdit(this.pageParams.contractNo).then(res => {
                                if (res.code === "0000") {
                                    //判断，之前申请过多少次还款日变更了，如果大于等于设置的变更次数，则需要收取手续费，否则不收取手续费
                                    if (res.data) {
                                        if (this.formData.repayChangeLimitFrequency - 1 > res.data.length) {
                                            this.formData.poundage = 0.00;
                                        }
                                    }
                                }
                            });
                            let s = moment(this.formData.oldDueDate).startOf('month');
                            let e = moment(this.formData.oldDueDate).endOf('month');
                            this.dateSetting = {
                                disabledDate(date) {
                                    return date && (date.valueOf() < s || date.valueOf() > e);
                                }
                            }
                        }
                    });
                }
            },
            /*
             * formData 数据验证 验证通过后 将数据传回父级处理  (保存/提交/审核 通用)
             * @param {String} funcName 回传给父级的方法 (注意父级需要传方法)
             * @param {Object} [data]   父级带来的数据
             */
            formValidate(funcName, data) {
                if (!funcName) return;

                this.$refs.formData.validate(valid => {
                    if (valid) {
                        let d = Object.assign({}, data, this.formData, this.Obj);

                        this.$emit(funcName, d);
                    } else {
                        this.$Message.error("验证不通过!");
                        this.$emit("closeSpin");
                        verifyErrorToView(this.$refs.formData.fields);
                    }
                });
            },
            /**
             * 发送短信给用户
             */
            sendSmgToContract() {

                let params = {
                    name: this.pageParams.custName,
                    number: this.Obj.phone,
                    contractNo: this.Obj.contractNo,
                    templateType: 'short-msg',
                    templateBusiness: 'repayment-date-change',
                    whetherCustom: 'no',
                    channel: '0'
                };
                console.log("params", params)
                sendMessageInfo(params).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.msg);
                    } else {
                        this.$Message.fail(res.msg);
                    }
                });
            },
            /*
             * 根据选择的新还款日，计算额外利息和费用总计
             */
            calculate(newDueDate) {
                const {poundage, oldDueDate, realInterestRate, remainPrinciple} = this.formData
                let nDue = moment(newDueDate).add( "days");
                let oDue = moment(oldDueDate);

                let overdueDay = nDue.diff(oDue, "day");

                let extraInterest = 0.00;
                let yearData = 36500;
                let nowYear = moment().format("YYYY");
                if((nowYear%4==0 && nowYear%100!=0) || nowYear%400==0){
                    yearData = 36600;
                }

                // let sumCost = "0";
                if (overdueDay > 0) {
                    //未偿本金*实际利率/36500*逾期天数
                    extraInterest = Math.round(remainPrinciple * realInterestRate / yearData * overdueDay * 100) / 100;
                    // sumCost = extraInterest + poundage;
                }
                this.formData = Object.assign({}, this.formData, {extraInterest});

            }
        },
    };
</script>
